package com.xin.di.uav.applet.rest;

import com.alibaba.fastjson.JSONObject;
import com.xin.di.uav.common.enums.ErrorEnum;
import com.xin.di.uav.common.exception.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author LTQ
 */
@RestController
@RequestMapping("/home/sms/")
public class SendSmsRest {

    private final static Logger logger = LoggerFactory.getLogger(SendSmsRest.class);
    @Autowired
    private RestTemplate restTemplate;

    @Value("${sms-url:https://notice-apis.sit.sf-express.com/unp/notice/single}")
    private String smsUrl;

    @Value("${sms.template-code:SHOU-JI-HAO-+-YAN-ZHENG-MA-DENG-LU}")
    private String templateCode;

    @Value("${sms.access-id:kw2sIDGE}")
    private String accessId;

    @Value("${sms.access-token:694e2cf1410f49bba2848dddf494ac14}")
    private String accessToken;

    @Value("${sms.new-access-id:ewMcZofY}")
    private String newAccessId;

    @Value("${sms.new-access-token:578526c6b925467fa574841d1e4c1a9f}")
    private String newAccessToken;

    public void sendSms(String phone, String code) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", phone);
        jsonObject.put("templateCode", templateCode);
        JSONObject templateParam = new JSONObject();
        templateParam.put("validateCode", code);
        jsonObject.put("templateParam", templateParam);
        jsonObject.put("accessId", accessId);
        jsonObject.put("accessToken", accessToken);

        HttpEntity<String> entity = new HttpEntity<>(jsonObject.toJSONString(), headers);
        ResponseEntity<JSONObject> response;
        response = restTemplate.postForEntity(smsUrl, entity, JSONObject.class);
        JSONObject result = response.getBody();
        if (!(Boolean) response.getBody().get("success")) {
            throw new BusinessException(ErrorEnum.SEND_SMS_FAIL, response.getBody().get("errorMessage"));
        }
        logger.info(result.toJSONString());
    }

    public void sendNotice(String phone, String templateCode, String tcWaybillNo, String place) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", phone);
        jsonObject.put("templateCode", templateCode);
        JSONObject templateParam = new JSONObject();
        templateParam.put("airportName", place);
        templateParam.put("tcWaybillNo", tcWaybillNo);
        jsonObject.put("templateParam", templateParam);
        jsonObject.put("accessId", newAccessId);
        jsonObject.put("accessToken", newAccessToken);

        HttpEntity<String> entity = new HttpEntity<>(jsonObject.toJSONString(), headers);
        ResponseEntity<JSONObject> response;
        response = restTemplate.postForEntity(smsUrl, entity, JSONObject.class);
        JSONObject result = response.getBody();
        if (!(Boolean) response.getBody().get("success")) {
            logger.info(response.getBody().get("errorMessage").toString());
            throw new BusinessException(ErrorEnum.SEND_SMS_FAIL, response.getBody().get("errorMessage"));
        }
        logger.info(result.toJSONString());
    }
}
